//
//  DynamicRedCountView.m
//  WeCommunity
//
//  Created by mac on 2017/6/14.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "DynamicRedCountView.h"

@interface DynamicRedCountView ()

@property (nonatomic,strong) UILabel * titleLabel;

@property (nonatomic,strong) UILabel * redCountLabel;

@end

@implementation DynamicRedCountView

-(instancetype)init
{
    if (self = [super init]) {
        self.backgroundColor = [UIColor colorWithHex:0xf1f1f1 alpha:1.0];
    }
    return self;
}

-(UILabel *)getCustomLabel
{
    UILabel * label = [[UILabel alloc] init];
    label.font = PFR_FONT(9);
    label.textColor = [UIColor colorWithHex:0x6e6e6e alpha:1.0];
    label.textAlignment = NSTextAlignmentRight;
    return label;
}

-(UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [self getCustomLabel];
        [self addSubview:_titleLabel];
    }
    _titleLabel.text = KNSLocal(@"readCount");
    return _titleLabel;
}

-(UILabel *)redCountLabel
{
    if (!_redCountLabel) {
        _redCountLabel = [self getCustomLabel];
        [self addSubview:_redCountLabel];
    }
    return _redCountLabel;
}

-(void)setRedCount:(long)redCount
{
    _redCount = redCount;

    self.redCountLabel.frame = CGRectMake(0, 0, KScreen_Width, 13);
    self.redCountLabel.text = [NSString stringWithFormat:@"%@%@",[self formatReadCount],KNSLocal(@"count")];
    self.redCountLabel.numberOfLines = 1;
    [self.redCountLabel sizeToFit];
    CGFloat width = self.redCountLabel.frame.size.width;
    if (width < 30) {
        width = 30;
    }
    self.frame = CGRectMake(KScreen_Width - 12 - width - 4, 10, width + 4, 26);
    self.titleLabel.frame = CGRectMake(2, 0, width, 13);
    self.redCountLabel.frame = CGRectMake(2, 13, width, 13);
}

-(NSString *)formatReadCount
{
    NSString * count;
    if (self.redCount > 10000) {
        count = [NSString stringWithFormat:@"%.1lfW",self.redCount / 10000.0];
    }else{
        count = [NSString stringWithFormat:@"%.0ld",self.redCount];
    }
    return count;
}

@end
